/*====================================================================
                                                                     
  RESEARCH:		
  
  BY:				                 													   	 		
  
  LAST UPDATED:	September 2022
                   
  DESCRIPTION:  Confidence intervals for data moments
  
  
*====================================================================*/	

clear all
set more off

//Change directory below!
cd ""

//Load generated moments from 100 replications

use "data/DataBootstrap.dta",clear

local varlist "num_suppliers num_buyers se_sales shr_sales frac_exporters exp_intensity uw_supwages w_supwages"

foreach v in `varlist'{
	gen sd_`v'=`v'
	gen mean_`v'=`v'
}

collapse (sd) sd_* (mean) mean_*,by(wageq)


merge 1:1 wageq using "data/ModelvsData"

foreach v in `varlist'{
	gen lb_`v'=`v'_data-(sqrt((1+1/100)*(sd_`v'^2)))*1.96 // adjust by the number of simulations
	gen ub_`v'=`v'_data+(sqrt((1+1/100)*(sd_`v'^2)))*1.96
}

tw (scatter num_suppliers_model wageq, mcolor(gs8)) (rcap lb_num_suppliers ub_num_suppliers wageq, lcolor(gs0)),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Number of suppliers",size(medium)) saving(num_suppliers.gph,replace)

tw (scatter num_buyers_model wageq, mcolor(gs8)) (rcap lb_num_buyers ub_num_buyers wageq, lcolor(gs0)),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Number of customers",size(medium)) saving(num_buyers.gph,replace)

tw (scatter shr_sales_model wageq, mcolor(gs8)) (rcap lb_shr_sales ub_shr_sales wageq, lcolor(gs0)),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Share of total network sales",size(medium)) saving(shr_sales.gph,replace)

tw (scatter se_sales_model wageq, mcolor(gs8)) (rcap lb_se_sales ub_se_sales wageq, lcolor(gs0)),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Standard deviation of log sales",size(medium)) saving(se_sales.gph,replace)

tw (scatter frac_exporters_model wageq, mcolor(gs8)) (rcap lb_frac_exporters ub_frac_exporters wageq, lcolor(gs0)),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Fraction of exporters",size(medium)) saving(frac_exporters.gph,replace)

tw (scatter exp_intensity_model wageq, mcolor(gs8)) (rcap lb_exp_intensity ub_exp_intensity wageq, lcolor(gs0)),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Export intensity of exporters",size(medium)) saving(exp_intensity.gph,replace)

tw (scatter uw_supwages_model wageq, mcolor(gs8)) (rcap lb_uw_supwages ub_uw_supwages wageq, lcolor(gs0)),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Uwgt. avg. log wage of suppliers",size(medium)) saving(uw_supwages.gph,replace)

tw (scatter w_supwages_model wageq, mcolor(gs8)) (rcap lb_w_supwages ub_w_supwages wageq, lcolor(gs0)),graphregion(color(white)) legend(off) xtitle("Wage quintiles") title("Wgt. avg. log wage of suppliers",size(medium)) saving(w_supwages.gph,replace)


gr combine num_suppliers.gph num_buyers.gph shr_sales.gph se_sales.gph  frac_exporters.gph exp_intensity.gph uw_supwages.gph w_supwages.gph, graphregion(color(white))
//gr export CIs.png,replace
gr export "output/figures/FigureA7.png",replace

foreach v in `varlist'{
	capture erase `v'.gph
}
